package com.golang.api.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.golang.api.domain.WithdrawLog;
import com.golang.api.dto.request.PageDto;
import com.golang.api.dto.request.UserAccountDto;
import com.golang.api.dto.request.WithdrawDto;
import com.golang.api.dto.response.WithdrawLogPageResp;
import com.golang.api.service.IUserAccountService;
import com.golang.api.service.IWithdrawLogService;
import com.golang.api.util.Result;
import com.golang.api.util.ResultGenerator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Slf4j
@RequestMapping("/api/user/withdraw")
public class WithdrawController {

    @Autowired
    private IWithdrawLogService withdrawLogService;

    /**
     * 提现申请
     *
     * @param withdrawalDto
     * @return
     */
    @PostMapping(value = "/apply")
    public Result apply(@ModelAttribute WithdrawDto withdrawalDto) {
        log.info("提现申请入参信息:{}", withdrawalDto);
        boolean success = withdrawLogService.apply(withdrawalDto);
        return ResultGenerator.success().setResult(success);
    }

    /**
     * 提现记录列表
     *
     * @param pageDto
     * @return
     */
    @PostMapping(value = "/record/listPage")
    public Result<WithdrawLogPageResp> recordPage(@ModelAttribute PageDto pageDto) {
        log.info("提现列表入参信息:{}", pageDto);
        Page<WithdrawLog> page = new Page<>(pageDto.getPageIndex(), pageDto.getPageSize());
        Page<WithdrawLog> withdrawLogPage = withdrawLogService.page(page, new LambdaQueryWrapper<WithdrawLog>().orderByDesc(WithdrawLog::getId));
        WithdrawLogPageResp withdrawLogPageResp = new WithdrawLogPageResp();
        if(null!=withdrawLogPage){
            withdrawLogPageResp.setWithdrawLogList(withdrawLogPage.getRecords());
            withdrawLogPageResp.setTotalCount(withdrawLogPage.getTotal());
        }
        return ResultGenerator.success().setResult(withdrawLogPageResp);
    }
}
